//app.js
var config = require('utils/config.js');
var netutil = require('utils/netutil.js');
var baseutil = require('utils/baseutil.js');
let { WeToast } = require('src/wetoast.js')
App({
  WeToast,
  globalData: {
    isLogin: false,
    userInfo: null,//微信用户信息
    TUserInfo: null,//商旅用户信息
    tempData: null,
    tempData2: null,
    openid: null,

    /**票务相关 */
    goDate:'',//出发日期
    fromCityName:'',//出发城市名
    toCityName:'',//到达城市名
    fromCity:'',//出发城市三字码
    toCity:'',//到达城市三字码
    
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    var THIS = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {//获取openid
        THIS.globalData.openid = res.data
        console.log("本地openid=" + THIS.globalData.openid)
        THIS.getCode();
      }, fail: function (res) {
        console.log(res)
        THIS.getCode();
      }
    })
    

  },

  getCode: function () {
    var THIS = this;

    if (THIS.globalData.openid!=null  ) {//openid是否保存
      THIS.judgeBind();
      console.log("openid已经保存")
      return;
    }

    wx.login({//获取code
      success: function (res) {
        console.log(res)
        if (res.code) {
          console.log('获取用户登录态成功！' + res.code)
          //获取openid
          // var url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxe23703293d5b48d7&secret=49a69e1efee06209130d4aa3e3df930e&js_code=" + res.code + "&grant_type=authorization_code"

          var url = config.getBaseUrl("GloUser", "GetXCXOpenID");
          baseutil.log(url);

          var Data = new Object();
          Data.Code = res.code;

          netutil.post(
            url, Data,
            function (res) {
              baseutil.log(res.data);
              if (res.data.ResponseMetaInfo.StatusCode == '0') {
                var openid = res.data.Data.OpenID;
                console.log("网络获取的=" + openid)
                // console.log(res.data.session_key)
                THIS.globalData.openid = openid;
                THIS.judgeBind();

                wx.setStorage({
                  key: "openid",
                  data: openid
                })
              } else {
                wx.showToast({
                  title: res.data.ResponseMetaInfo.Reason,
                  icon: 'success',
                  duration: 1000
                })
              }
            },
            function (res) {
              baseutil.log(res.data)
            })

        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },
  judgeBind: function () {

    var THIS = this
    var url = config.getBaseUrl("GloUser", "XCXLogin")

    var Data = new Object();
    Data.OpenID = THIS.globalData.openid;

    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var Data = res.data.Data;
          THIS.globalData.TUserInfo = Data
          THIS.globalData.isLogin = true
          baseutil.saveUserInfo(Data)

        } else {
          baseutil.log(res.data.ResponseMetaInfo.Reason);
          wx.getStorage({//判断是否保存了微信用户信息
            key: 'userHead',
            success: function (res) {
              console.log(res.data);
            },
            fail: function (res) {
              console.log(res.errMsg + "------------");
              THIS.getUserInfo(THIS.globalData.userInfo)
            }
          })
        }
      },
      function (res) {

      }
    )
  },
  getUserInfo: function (cb) {
    var that = this

    //调用登录接口
    wx.getUserInfo({
      withCredentials: false,
      success: function (res) {

        console.log(res)
        //nickName
        wx.setStorage({
          key: "nickName",
          data: res.userInfo.nickName
        })
        wx.setStorage({
          key: "userHead",
          data: res.userInfo.avatarUrl
        })
      }
    })

  },

})